package com.sq.partner.manager.controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.mybatis.model.GatewayEinvoice;
import com.sq.partner.manager.service.EinvicePrintService;
import com.sq.partner.manager.service.INlifeCoreProdService;
import com.sq.partner.manager.util.LogUtil;
	/**
	 *  申请电子发票
	 */
	@Controller
	@RequestMapping("/einvoice")
	public class EinvoiceMangerController {
		private static Logger logger = LogUtil.getLog(EinvoiceMangerController.class);
		@Resource
		private INlifeCoreProdService nlifeCoreProdService;
		@Resource
		private EinvicePrintService einvicePrintService;
		
		@RequestMapping(value = "/show")
		public String list() throws Exception {
			logger.debug("forward newLifeCoreProduct successful....");
			return "actirationCard/activatedCard/einvoice/getEinvoice";
		}
		
		@RequestMapping(value = "/getEinvoice")
		@ResponseBody
		public Map<String, Object> getEinvoice(String polNo,String einvoiceName,String taxNo,String phoneNo,String email) throws Exception {
			HashMap<String, Object> jmap = new HashMap<String, Object>();
			GatewayEinvoice einvoice = null;
			einvoice = einvicePrintService.geteinvoice(polNo);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("polNo", polNo);
			map.put("einvoiceName",einvoiceName);
			map.put("taxNo", taxNo);
			map.put("phoneNo", phoneNo);
			map.put("email", email);
			/**发票未申请*/
			if (einvoice==null||"4".equals(einvoice.getEinvoiceStatus())) {
				Map<String, Object> returnMap = einvicePrintService.reqEinvice(map);
				if ("1".equals(returnMap.get("result"))) {
					jmap.put("result", true);
					jmap.put("msg", "电子发票开具申请已提交，请稍后点击【电子发票开具查询】");
					return jmap;
				}else{
					jmap.put("result", false);
					jmap.put("msg", returnMap.get("message").toString());
					return jmap;
				}
			}else{
				jmap.put("result", true);
				jmap.put("msg", "该单发票已申请，请直接进行查询");
				return jmap;
			}
		}
		
		@RequestMapping(value = "/queryEinvoice")
		@ResponseBody
		public Map<String, Object> queryEinvoice(String polNo) throws Exception {
			HashMap<String, Object> jmap = new HashMap<String, Object>();
			GatewayEinvoice einvoice =  einvicePrintService.geteinvoice(polNo);
			if (einvoice==null||"1".equals(einvoice.getEinvoiceStatus())) {
				jmap.put("result", false);
				jmap.put("errInfo", "发票未申请，请申请电子发票！");
				return jmap;
			}else if ("2".equals(einvoice.getEinvoiceStatus())) {
				jmap.put("result", false);
				jmap.put("errInfo", "发票申请中，请稍后再试！");
				return jmap;
			}else if("3".equals(einvoice.getEinvoiceStatus())){
				jmap.put("result", true);
				HashMap<String, Object> einvMap = new HashMap<String, Object>();
				einvMap.put("einvoiceNo", einvoice.getEinvoiceNo());
				einvMap.put("code", einvoice.getEinvoiceCode());
				einvMap.put("status","发票开具成功");
				einvMap.put("url", einvoice.getEinvoiceUrl());
				jmap.put("einvMap", einvMap);
				return jmap;
			}else{
				jmap.put("result", false);
				jmap.put("errInfo", "发票开具失败，失败原因："+einvoice.getErrInfo());
				return jmap;
			}
		}
		
		private String generateSerialNumber(String prodType) throws Exception {
			Calendar now = Calendar.getInstance();
			String suffixStr = now.get(Calendar.YEAR) + "" + now.get(Calendar.MONTH) + "" + now.get(Calendar.DAY_OF_MONTH) + "" + now.getTimeInMillis();
			return prodType + "@" + suffixStr;
		}
	}

	
	